const QiniuU = require("../lib").QiniuUniversally;
const path = require("path");
const filesize = require("filesize");
const https = require("https");
const soyoung = require("../lib/simple_login/soyoung.js");

process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = 0;
// let requ = https.request({
//   host: "nyaa.si",
//   method: "get",
//   headers: {
//     host: "sukebei.nyaa.si",
//     // "Accept-Encoding":"deflate"
//   }
// }, (httpRes) => {
//   console.log(httpRes.headers);
//   // httpRes.pipe(process.stdout);
//   httpRes.on("data",(d)=>{
//     // debugger
//   })
// });
// requ.end();
// requ.on("error",(err)=>{
//   // debugger
// })


let q = new QiniuU;
QiniuU.__debug.TokenGetter.get_tuniu().then(async token_ => {
  // debugger
  let t1 = Date.now();
  let o_t = await soyoung.getQiniuToken();
  let t2 = Date.now();
  let o_t2 = await soyoung.getQiniuToken();
  let t3 = Date.now();
  // debugger
  // q.do_upload_dir(path.join(__dirname, "../tmp/link/"), undefined, undefined).then(o => {
  //   let pianke_html = o.data.pianke.toHTML();
  //   let m_meipian = o.data.meipian;
  //   m_meipian.upload_ok_paths = [{
  //     path: "/a/b/c.file",
  //     link: "https://www.baidu.com"
  //   }];
  //   m_meipian.mkblk_failed_paths = [{
  //     path: "/a/v/wdqff.file",
  //     reason: '[Intervention] Slow network is detected. See https://www.chromestatus.com/feature/5636954674692096 for more details. Fallback font will be used while loading: https://cdn2.bearychat.com/chat/static/media/bearyfont.cead1910.woff'
  //   }];
  //   m_meipian.mkfile_failed_paths = [{
  //     path: "/a/v/wdqff.file",
  //     reason: '[Intervention] Slow network is detected. See https://www.chromestatus.com/feature/5636954674692096 for more details. Fallback font will be used while loading: https://cdn2.bearychat.com/chat/static/media/bearyfont.cead1910.woff'
  //   }];
  //   debugger
  // });
  // q.do_upload_file(path.join(__dirname, "../tmp/link/test.rss")).then(o => {
  //   debugger
  // })
  // debugger
  const BBT = require("../lib/simple_login/babytree");
  let o_token = await BBT.getToken()
  let o_ = await QiniuU.__debug.TokenGetter.get_tuniu();
  // debugger
  (async $ => {
    q.mkblk_4_file(path.join(__dirname, "../tmp/54MBmp4.sf-part1.mp4")).then(async o => {
      // console.log(JSON.stringify(o.data));
      // debugger
      // await QiniuU.__debug.TokenGetter.get_daovoice();
      let o_du_hupu = await QiniuU.__debug.FileMakers.mkfile_meipian(
        { path: "path/to/file", data: o.data },
        4 * 1024 * 1024
      )
      debugger
      let o_udesk = await QiniuU.__debug.FileMakers.mkfile_udesk(
        { path: "path/to/file", data: o.data },
        20 * 1024 * 1024
      )
      debugger
      let o_7moor = await QiniuU.__debug.FileMakers.mkfile_7moor({ path: "path/to/file", data: o.data },
        8 * 1024 * 1024);
      let o_pianke = await QiniuU.__debug.FileMakers.mkfile_pianke({ path: "path/to/file.mp4", data: o.data },
        12 * 1024 * 1024);
      let o_hunliji = await QiniuU.__debug.FileMakers.mkfile_hunliji({ path: "path/to/file.mp4", data: o.data },
        12 * 1024 * 1024);
      let o_owhat = await QiniuU.__debug.FileMakers.mkfile_owhat({ path: "path/to/file.mp4", data: o.data },
        12 * 1024 * 1024);
      debugger
    });
  })();

});



/**
 * @returns {String}
 * @param {String} full_path 
 * @param {Number} part_index 
 * @param {Number} part_counts 
 */
function TARED_NAMING_FUNCRTION(full_path, part_counts, part_index) {
  if (part_counts == 1) {
    return path.basename(full_path) + ".tar";
  } else {
    let max_length = String(part_counts).length;
    if (max_length < 3) max_length = 3;
    let str_index = String(part_index);
    while (str_index.length < max_length) {
      str_index = `0${str_index}`
    }
    return `${path.basename(full_path)}.${str_index}.tar`;
  }
}

// q.mkblk_4_dir(path.join(__dirname,"../tmp/")).then(o=>{
//     debugger
// });
